using UnityEngine;
using System.Collections;

/// \class  gkAbyssalGiantSubmarineBubbleParticleController
/// \brief  The controller of the bubble particles
[AddComponentMenu("GK/AbyssalGiant/Submarine/gkAbyssalGiantSubmarineBubbleParticleController")]
public class gkAbyssalGiantSubmarineBubbleParticleController : MonoBehaviour 
{
	/// The normal particle system component
	public GameObject ParticleSystemNormal;
	
	/// The boost system component
	public GameObject ParticleSystemBoost;
	
	/// Is the submarine boosting
	private bool m_bBoosting;

	/// The scrolling info
    private gkScrollingInfo m_rScrollingInfo;
	
	/// \brief  Called just after the game begin to play
	void Start()
	{			
		// Init the particle systems
		SetupParticleSystems();
	}
	
	/// \brief  Called when the component is enabled
	void OnEnable() 
	{
		// Grab the scrolling info
		m_rScrollingInfo = GameObject.Find("ScrollingInfo").GetComponent<gkScrollingInfo>();
		
		// Listen the boost event of the scrolling info
		m_rScrollingInfo.GetComponent<gkEventComponent>().AddEventHandler(gkBoostEvent.BoostBegin, HandleBoostBeginEvent);
		m_rScrollingInfo.GetComponent<gkEventComponent>().AddEventHandler(gkBoostEvent.BoostEnd, HandleBoostEndEvent);
	}
	
	/// \brief  Called when the component is disabled
	void OnDisable() 
	{	
		// Remove the boost event of the scrolling info
		if(m_rScrollingInfo != null)
		{
			m_rScrollingInfo.GetComponent<gkEventComponent>().RemoveEventHandler(gkBoostEvent.BoostBegin, HandleBoostBeginEvent);
			m_rScrollingInfo.GetComponent<gkEventComponent>().RemoveEventHandler(gkBoostEvent.BoostEnd, HandleBoostEndEvent);
		}
	}
	
	/// \brief  Handle the boost event
	void HandleBoostBeginEvent(Object a_rEvent)
	{
		if(m_rScrollingInfo.IsAccelerated())
		{
			SetBoosting(true);
		}
		else
		{
			SetBoosting(false);
		}
	}
	
	/// \brief  Handle the boost event
	void HandleBoostEndEvent(Object a_rEvent)
	{
		SetBoosting(false);
	}
		
	
	/// \brief  Set if the submarine is boosting or not
	void SetBoosting(bool a_bBoosting)
	{
		if(m_bBoosting != a_bBoosting)
		{
			m_bBoosting = a_bBoosting;
			SetupParticleSystems();
		}
	}
	
	/// \brief  Setup the particle system
	void SetupParticleSystems()
	{
		if(m_bBoosting)
		{
			ParticleSystemNormal.particleEmitter.emit = false;
			ParticleSystemBoost.particleEmitter.emit = true;
		}
		else
		{
			ParticleSystemNormal.particleEmitter.emit = true;
			ParticleSystemBoost.particleEmitter.emit = false;
		}
	}
}